/* 请求体配置 */
import axios from 'axios'
import { message } from 'antd'
import router from 'umi/router'

let number = 0
let errNumber = 0

export default function Request(options = {}) {
  // 获取token
  const access_token = sessionStorage.getItem('loginInfo') && 'Bearer ' + JSON.parse(sessionStorage.getItem('loginInfo')).access_token || ''

  /* 请求头 */
  const headers = {
    'Content-Type': options.isForm ? 'multipart/form-data' : 'application/json',
    'Authorization': options.Authorization ? options.Authorization : access_token,
  };

  // 拼接GET请求头部
  const getUrl = () => {
    if (options.data && JSON.stringify(options.data) !== '{}') {
      for(let key in options.data){
        return options.url + '/' + options.data[key]
      }
    }
    return options.url
  }
  // 开发环境URL
  const developmentUrl = () => {
    return options.method ? (options.method === 'DELETE' ? getUrl() : options.url) : getUrl()
  }
  // 生产环境URL
  const befor = options.url.slice(0,4)
  const after = options.url.slice(4,options.url.length)
  const productionUrl = (key) => {
    switch(key) {
      case '/usr':
        return '/authorization-server' + after; // 8000
      case '/msg':
        return after; // 8443
      case '/api':
        return '/equipment' + after; // 8030
      case '/rep':
        return '/trade' + after; // 8020
      case '/apc':
        return '/equipment' + after; // 8030
      case '/apg':
        return '/organization' + after; // 8010
      case '/apd':
        return '/trade' + after; // 8020
      case '/apa':
        return '/fileCenter' + after; // 8040
      case '/apu':
        return '/minio' + after; // 8033
    }
    // switch(key) {
    //   case '/usr':
    //     return 'http://121.196.179.144:8000' + after; // 8000
    //   case '/msg':
    //     return 'http://121.196.179.144:8443' + after; // 8443
    //   case '/api':
    //     return 'http://121.196.179.144:8030' + after; // 8030
    //   case '/rep':
    //     return 'http://121.196.179.144:8020' + after; // 8020
    //   case '/apc':
    //     return 'http://121.196.179.144:8030' + after; // 8030
    //   case '/apg':
    //     return 'http://121.196.179.144:8010' + after; // 8010
    //   case '/apd':
    //     return 'http://121.196.179.144:8020' + after; // 8020
    //   case '/apa':
    //     return 'http://121.196.179.144:8040' + after; // 8040
    //   case '/apu':
    //     return 'http://121.196.179.144:8033' + after; // 8033
    // }
  }

  // 判断生产环境还是开发环境
  const urlAddress = process.env.NODE_ENV === 'development' ? developmentUrl() : productionUrl(befor);

  const httpRequestBody = {
    headers: headers,
    method: options.method || 'GET',
    url: urlAddress,
    // withCredentials: false,
    timeout: 1800000,
  };
  // 判断参数类型加时间戳
  if (Object.prototype.toString.call(options.data) === '[object Object]') {
    options.data.t = +new Date()
  }
  if (Object.is(httpRequestBody.method, 'POST') || Object.is(httpRequestBody.method, 'PUT')) {
    httpRequestBody.data = options.data || '';
  } else {
    httpRequestBody.params = options.data || '';
  }


  return axios(httpRequestBody).then((res) => {
    ////当有mesg时候，返回原接口处理数据，等没有mesg时 可能是无账号信息 则直接跳到登录页面
    let data=res
    if (res.data ) {
      data= res.data
    }

    if (data.code === '040005') {
      sessionStorage.clear()
      localStorage.clear()
      if (data.mesg) {
        // 解决工作台7次提示
        if (number === 0) {
          number = number + 1
          message.error(data.mesg, 1.5, () => {
            router.push('/login')
          })
        }
      } else {
        router.push('/login')
      }
      return
    } else if (data.code === '040006') {
      if (data.mesg) {
        if (number === 0) {
          number = number + 1
          message.error(data.mesg)
        }
      }
    } else {
      number = 0
      errNumber = 0
      return data
    }
  }).catch(res => {
    if (
      res
      && res.response
      && res.response.config
      && res.response.config.url
      && res.response.config.url.includes('getRepairOrderCount1')) {
      if (errNumber === 0) {
        errNumber = errNumber + 1
        message.error('网络出错，请稍后再试')
      }
    }
  })
}
